package com.datatrans.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javafx.scene.transform.Rotate;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class DataTransform {
    public static void main(String[] args) throws IOException {
        String text = FileUtils.readFileToString(new File("./input/concepts.txt"));

        // 1. 解析文本
        String fullText = parseText(text);
        System.out.println(fullText);

        String[] formattedLines = fullText.split("\n");

        System.out.println("-----");
        ConceptTree root = new ConceptTree("");
        ConceptTree currentConceptTree = root;

        for (String formattedLine : formattedLines) {
            String[] concepts = formattedLine.split(",");
            for (String concept : concepts) {
                currentConceptTree = root.addConcept(currentConceptTree, concept);
            }
            currentConceptTree = root;
        }

        // 2. 递归遍历输出
        System.out.println(root.toString());
    }

    private static String parseText(String text) {
        StringBuilder builder = new StringBuilder();

        String[] lines = text.split("\n");
        String[] formattedLines = new String[lines.length];

        // 遍历所有行，并使用回溯的方式填充占位符
        for(int i = 0; i < lines.length; ++i) {
            String line = lines[i];
            if(i == 0) {
                formattedLines[i] = line;
            }
            else if(!line.startsWith(",")) {
                formattedLines[i] = line;
            }
            // 只有首字符是逗号才需要解析
            else{
                // 回溯获取上一个行
                String previousFormattedLine = formattedLines[i - 1];
                String[] words = previousFormattedLine.split(",");
                // 迭代替换行中的逗号,并标记
                int c = 0;
                int index = -1;
                formattedLines[i]  = "";

                while(c < line.length()) {
                    if(line.charAt(c) == ',') {
                        index++;
                        formattedLines[i] += words[index] + ",";
                    }
                    else {
                        formattedLines[i] += line.substring(c);
                        break;
                    }
                    c++;
                }
            }
        }

        for (String formattedLine : formattedLines) {
            builder.append(formattedLine + "\n") ;
        }

        return builder.toString();
    }

    /**
     * 知识树
     */
    static class ConceptTree {
        private String concept;
        private List<ConceptTree> children;

        public ConceptTree(String data) {
            this.concept = data;
            children = new ArrayList();
            children.clear();
        }

        /**
         * 添加知识点到知识树
         * @param parentConcept
         * @param concept
         * @return
         */
        public ConceptTree addConcept(ConceptTree parentConcept, String concept) {
            ConceptTree conceptTree = searchByConcept(parentConcept, concept);
            if(conceptTree == null) {
                ConceptTree newConcept = new ConceptTree(concept);
                parentConcept.children.add(newConcept);
                return newConcept;
            }

            return conceptTree;
        }

        /**
         * 递归查询知识点
         * @param conceptTree 父知识树
         * @param concept 知识点
         * @return
         */
        public ConceptTree searchByConcept(ConceptTree conceptTree, String concept) {
            // 判断根节点是否匹配
            if(conceptTree.concept.equals(concept)) {
                return conceptTree;
            }

            if(conceptTree.children != null && conceptTree.children.size() > 0) {
                // 判断子节点是否匹配
                for (ConceptTree child : conceptTree.children) {
                    if (child.children.equals(concept)) {
                        return child;
                    }
                }

                // 递归子节点判断是否匹配
                for (ConceptTree child : conceptTree.children) {
                    ConceptTree searchResult = searchByConcept(child, concept);
                    if (searchResult != null) {
                        return searchResult;
                    }
                }
            }

            return null;
        }

        /**
         * 递归遍历以JSON形式打印整棵树
         * @return
         */
        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();

            if(children.size() > 0) {
                builder.append("{");

                for (ConceptTree child : children) {
                    builder.append("\"" + child.concept + "\":");
                    builder.append("[");
                    builder.append(child.toString());
                    builder.append("]");
                    builder.append(",");
                }
                builder.delete(builder.length() - 1, builder.length());
                builder.append("}");
            }

            return builder.toString();
        }
    }
}
